package chapter14.Homework;

import java.util.ArrayList;

import java.util.Collections;
import java.util.Iterator;

/**
 * @Auther: Tx
 * @Date: 2022/5/25 22:30
 */
@SuppressWarnings("all")
public class HomeWork01 {
    public static void main(String[] args) {
        news news1 = new news("新闻一：新冠确诊病例超千万，数百万印度教信徒奔赴横河“圣浴”引起担忧");
        news news2 = new news("新闻二：男子突然想起两个月前钓的鱼还在网兜里，捞起一看赶紧放生");
//        1.将新闻对象添加到ArrayList集合中，并且进行倒序遍历
        ArrayList arrayList = new ArrayList();
        arrayList.add(news1);
        arrayList.add(news2);
        Collections.reverse(arrayList);
        System.out.println(arrayList);
//        2.遍历集合过程中，对新闻标题进行处理；超过15字只保留前15个，之后添加...
        for (int i = arrayList.size()-1;i>=0;i--){
            news news = (news)arrayList.get(i);
            System.out.println(process(news.getTitle()));
        }


    }

   public static String process(String title) {
        if (title == null)
            return "";
        if (title.length() > 15) {
            return title.substring(0, 15) + "...";
        } else {
            return title;
        }
    }
}

class news {
    private String title;
    private String content;

    public news(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "news{" +
                "title='" + title + '\'' +
                ", content='" + content + '\'' +
                '}';
    }
}